var uploadImage = require('../../js/uploadImg/uploadAliyun.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPopup: false,
    is_submit: true,
    option:[],
    project_list:[],
    relationOption:[],
    maintain_list:[],
    do_type:0,
    capture:['camera'],
    project_id:0,
    maintainId:'',
    note_text: '',
    task_id:0,
    fileList: [],
    token:'',
    filePath: '',
    longitude: '',
    latitude: '',
    is_desc: 0,
    pn:'',
    ruleForm: {
      all_project:false,
      project_id: '',
      maintain_id: '',
      do_uid: 0,
      radio_type: 1,
      is_desc: 0,
      finish_date: '',
      maintain_name_id: 0,
      project_select:'',
      name: ''
    },
    current_location_str: '',
    count_src: 0,
    s_file_list: [],
    u_file_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      task_id: options.task_id,
      project_id: options.id,
      is_desc: options.is_desc,
      do_type:options.do_type,
      maintainId:options.maintainId,
      pn:options.pn,
      capture:options.do_type=='1'?['album', 'camera']:['camera']
    });
    if (options.do_type == 4 || options.do_type == 5) {
      wx.setNavigationBarTitle({
        title: (options.do_type == 4)?'上班考勤':'下班考勤' 
      })
    }else if (options.do_type == 3) {
      wx.setNavigationBarTitle({
        title: "拍照反馈" 
      })
    } else {
      wx.setNavigationBarTitle({
        title: options.pn 
      })
    }

  },
  goTodoTask() {
    wx.navigateTo({
      url: `/pages/doTask/index?s_file_list=${encodeURIComponent(JSON.stringify(this.data.s_file_list))}&u_file_list=${encodeURIComponent(JSON.stringify(this.data.u_file_list))}&pn=${this.data.pn}&id=${this.data.project_id}&task_id=${this.data.task_id}&is_desc=${this.data.is_desc}&do_type=${this.data.do_type}&maintainId=${this.data.maintainId}`,
    })
  },
 uploadImg(filePath){
  wx.showLoading({
    title: '照片上传中~',
    mask: true
  })
  var scope = this;
    //上传图片
    //你的域名下的/images/当前年月日文件下的/图片.png
    //图片路径可自行修改【(这二个参数就是你oss地址目录的下一个路径目录，比如:https://xxx.com/images/xxx.png)】
  uploadImage({
      filePath: filePath,
      dir: "lujinghua/",
      success: function (res) {
        let fileList = scope.data.u_file_list
        let sfileList = scope.data.s_file_list
        sfileList.push({ url: filePath });
        fileList.push({ url: res.imageURL });
        scope.setData({
          src: filePath,
          s_file_list: sfileList,
          u_file_list: fileList,
          count_src: scope.data.count_src+1
        })
        wx.hideLoading({
          complete: (res) => {},
        })
      },
      fail: function (res) {
        wx.hideLoading({
          complete: (res) => {},
        })
        wx.showModal({
          title: "温馨提示", // 提示的标题
          content: "上传失败，请重试！", // 提示的内容
          showCancel: false, // 是否显示取消按钮，默认true
          confirmText: "确定", // 确认按钮的文字，最多4个字符
          confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
          success: function (res) {
            return
          }
        })
      }
    })
 },

  init() {
  
  },
  takePhoto() {
    if(this.data.count_src > 8) {
      wx.showModal({
        title: "温馨提示", // 提示的标题
        content: "已经超过9张图片，请点击完成", // 提示的内容
        showCancel: false, // 是否显示取消按钮，默认true
        confirmText: "确定", // 确认按钮的文字，最多4个字符
        confirmColor: "#576B95", // 确认按钮的文字颜色，必须是 16 进制格式的颜色字符串
        success: function (res) {
          return
        }
      })
      return
    }
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'normal',
      success: (res) => {
        this.uploadImg(res.tempImagePath)
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})